import grails.util.Environment
import dccsuite.stock.Country
import dccsuite.stock.Gauge
import dccsuite.stock.RailwayCompany
import dccsuite.stock.Stock
import dccsuite.stock.StockElementManufacturer

class BootStrap {

	def fixtureLoader
	
    def init = { servletContext ->
		if (Environment.getCurrent() == Environment.DEVELOPMENT) {
			
			def csv = new File("grails-app/conf/data/cow.csv")
			csv.splitEachLine(';') { row ->
				if (!row[0].startsWith("#")) {
					new Country(
						isoCode: 	row[0],
						name:		row[10]
					).save(failOnError: true, flush: true)
				}
			}
			
//			new File("grails-app/conf/data/cow.csv").eachCsvLine { tokens ->
//				if (!tokens[0].startsWith("#"))  {
//					new Country(
//						isoCode: 	tokens[0],	// ISO 3166 2 Code
//						name:		tokens[4]	// Name
//					).save()
//				}
//			}
			
			
			/*fixtureLoader.load {
				build {
					spain(Country) {
						name = "Spain"
					}
						
					england(Country) {
						name = "England"
					}
						
					germany(Country) {
						name = "Germany"
					}
					
					usa(Country) {
						name = "USA"
					}
					
					nGauge(Gauge) {
						name = "N"
						acronym = "N"
						description = "N"
						relation = 160.0
					}
					
					h0Gauge(Gauge) {
						name = "H0"
						acronym = "H0"
						description = "H0"
						relation = 52.0
					}
					
					renfe(RailwayCompany) {
						name = "Renfe"
						description = "Renfe"
						country = spain
					}
					
					unionPacific(RailwayCompany) {
						name = "Union Pacific"
						description = "Union Pacific"
						country = usa
					}
					
					marklin(StockElementManufacturer) {
						name = "Marklin"
						description = "Marklin"
						country = germany
					}
					
					roco(StockElementManufacturer) {
						name = "Roco"
						description = "Roco"
						country = germany
					}
					
					arnold(StockElementManufacturer) {
						name = "Arnold"
						description = "Arnold"
						country = germany
					}
					
					startrain(StockElementManufacturer) {
						name = "Startrain"
						description = "Strtrain"
						country = spain
					}
					
					electrotren(StockElementManufacturer) {
						name = "Electrotren"
						description = "Electrotren"
						country = spain
					}
					
					ibertren(StockElementManufacturer) {
						name = "Ibertren"
						description = "Ibertren"
						country = spain
					}
					
					testStock(Stock) {
						name = "Test Stock"
						description = "Test Stock"
					}
				}
			}*/
		}
    }
	
    def destroy = {
		
    }
}
